package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.course.CourseDTO;
import com.qk.entity.Course;
import com.qk.management.aop.anno.LogAnno;
import com.qk.management.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: ZgYn
 * @Date 2025/10/8 19:48
 * @name：CourseController
 */
@RestController
@RequestMapping("/courses")
public class CourseController {

    @Autowired
    private CourseService courseService;

    @GetMapping
    public Result getCourses(CourseDTO courseDTO){
        PageResult<Course> courses = courseService.getCourses(courseDTO);
        return Result.success(courses);
    }

    @LogAnno
    @PostMapping
    public Result save(@RequestBody Course course){
        courseService.save(course);
        return Result.success();
    }

    @LogAnno
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        courseService.delete(id);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        Course course = courseService.getById(id);
        return Result.success(course);
    }

    @LogAnno
    @PutMapping
    public Result update(@RequestBody Course course){
        courseService.update(course);
        return Result.success();
    }

    @GetMapping("/list")
    public Result list(){
        List<Course> courses = courseService.list();
        return Result.success(courses);
    }

    @GetMapping("/subject/{subject}")
    public Result listBySubject(@PathVariable String subject){
        List<Course> courses = courseService.listBySubject(subject);
        return Result.success(courses);
    }
}
